home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 …ember: Reference Library / Dev.CD Dec 96 RL / Dev.CD Dec 96 RL.toast / Technical Documentation / develop / develop Issue 24 / develop Issue 24 code / Scriptable Database 1.0a15.sea / Scriptable Database 1.0a15 / Base / Object.cp / Object.cp
Encoding:
Text File  |  1996-02-20  |  9.1 KB  |  299 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Object.c
  3.  
  4.     Contains:    base class for many other classes
  5.  
  6.     Written by:    Bruce Horn, Steve Capps, Larry Kenyon, John Meier, scott douglass, Darin Adler,
  7.                 Paul Mercer, Bryan Stearns, Dave Owens
  8.                 
  9.                 Andy Nicholas
  10.  
  11.     Copyright:    © 1988-1995 by Apple Computer, Inc., all rights reserved.
  12.  
  13.          <5>     6/23/95    ga        
  14. */
  15.  
  16. #ifdef MWTRACEBACKTABLES
  17. #pragma traceback on
  18. #endif
  19.  
  20. #include "Object.h"
  21. #include "Exceptions.h"
  22. #include "Behavior.h"
  23.  
  24. //
  25. // For CopyMemory
  26. //
  27. #include "AbstractData.h"
  28.  
  29.  
  30. //========================================================================================
  31. // CLASS TClassData
  32. //========================================================================================
  33.  
  34. //
  35. // clObject isn't too useful, but here it is all the same.
  36. //
  37. #define clObject 1
  38. #define kAnyClass '****'
  39.  
  40. //
  41. // Most classes would use the ImplementClassData macro here;
  42. // TObject cannot use it, though, because it has no superclass.
  43. //    
  44. #ifdef DEBUG
  45.                                                                                                                                                                                                                                             
  46. TClassData TObject::fClassData = { "TObject", kAnyClass, clObject, sizeof(TObject), nil, {0, nil} };
  47.  
  48. #else
  49.  
  50. TClassData TObject::fClassData = { kAnyClass, clObject, sizeof(TObject), nil, {0, nil} };
  51.  
  52. #endif
  53.  
  54.  
  55. //========================================================================================
  56. // CLASS TObject
  57. //========================================================================================
  58.  
  59. //----------------------------------------------------------------------------------------
  60. // TObject::~TObject: 
  61. //----------------------------------------------------------------------------------------
  62. TObject::~TObject()
  63.     {
  64.     // Stub
  65.     }
  66.  
  67. //----------------------------------------------------------------------------------------
  68. // TObject::ClassData
  69. //----------------------------------------------------------------------------------------
  70. const TClassData* TObject::ClassData() const
  71.     {
  72.     return &fClassData;
  73.     } // TObject::ClassData
  74.  
  75. //----------------------------------------------------------------------------------------
  76. // TObject::Clone: 
  77. //----------------------------------------------------------------------------------------
  78. TObject* TObject::Clone()
  79.     {
  80.     UInt32 newObjectSize = this->ObjectSize();
  81.  
  82.     TObject* clonedObject = (TObject*) new char[newObjectSize];    
  83.  
  84.     //
  85.     // We should really use operator=
  86.     //
  87.     CopyMemory((Ptr)this, (Ptr)clonedObject, newObjectSize);
  88.     clonedObject->CloneOwnedObjects();
  89.     
  90.     return clonedObject;
  91.     } // TObject::Clone 
  92.  
  93. //----------------------------------------------------------------------------------------
  94. // TObject::CloneOwnedObjects
  95. //
  96. // This is where derived classes have a chance to clone their
  97. // owned objects after being cloned themselves.  This method is
  98. // called from TObject::Clone, and should not be called from anywhere
  99. // else.
  100. //----------------------------------------------------------------------------------------
  101. void TObject::CloneOwnedObjects()
  102.     {
  103.     } // TObject::CloneOwnedObjects
  104.  
  105. //----------------------------------------------------------------------------------------
  106. // TObject::SortOrder: 
  107. //----------------------------------------------------------------------------------------
  108. Boolean TObject::SortOrder(const TObject* object, void*) const
  109.     {
  110.     return (*this < *object);
  111.     }
  112.  
  113. //----------------------------------------------------------------------------------------
  114. // TObject::Key: 
  115. //----------------------------------------------------------------------------------------
  116. SInt32 TObject::Key() const
  117.     {
  118.     return 0;
  119.     }
  120.  
  121. //----------------------------------------------------------------------------------------
  122. // operator==: 
  123. //----------------------------------------------------------------------------------------
  124. Boolean    operator==(const TObject& first, const TObject& second)
  125.     {
  126.     return (first.Key() == second.Key());
  127.     }
  128.  
  129. //----------------------------------------------------------------------------------------
  130. // operator<: 
  131. //----------------------------------------------------------------------------------------
  132. Boolean    operator<(const TObject& first, const TObject& second)
  133.     {
  134.     return (first.Key() < second.Key());
  135.     }
  136.  
  137. #ifdef DEBUG
  138.  
  139. //----------------------------------------------------------------------------------------
  140. // TObject::SetupObjectDebugInfo
  141. //
  142. // We cannot set up the debugging information in the constructor because the object's
  143. // vtable will _always_ point at TObject in TObject::TObject.  We have to wait for
  144. // post-constructor time so that we can get the class data from the most-derived object.
  145. //----------------------------------------------------------------------------------------
  146. void TObject::SetupObjectDebugInfo()
  147.     {
  148.     fClassDataPtr = ClassData();
  149.     fSignature = ClassData()->ObjectClass();
  150.     }
  151.  
  152. //----------------------------------------------------------------------------------------
  153. // TObject::ClassID
  154. //
  155. // Inlined in non-debug builds
  156. //----------------------------------------------------------------------------------------
  157. SInt32 TObject::ClassID() const
  158.     {
  159.     ((TObject*)this)->SetupObjectDebugInfo();
  160.  
  161.     return ClassData()->ClassID();
  162.     }
  163.     
  164. //----------------------------------------------------------------------------------------
  165. // TObject::ObjectSize
  166. //
  167. // Inlined in non-debug builds
  168. //----------------------------------------------------------------------------------------
  169. UInt32 TObject::ObjectSize() const
  170.     {
  171.     ((TObject*)this)->SetupObjectDebugInfo();
  172.  
  173.     return ClassData()->ObjectSize();
  174.     }
  175.  
  176. #endif
  177.  
  178. //----------------------------------------------------------------------------------------
  179. // TObject::DerivedFrom: 
  180. //----------------------------------------------------------------------------------------
  181. Boolean    TObject::DerivedFrom(SInt32 cast) const
  182.     {
  183.     const TClassData* cd = this->ClassData();
  184.  
  185. #ifdef DEBUG
  186.     ((TObject*)this)->SetupObjectDebugInfo();
  187. #endif
  188.     
  189.     while (cd != nil)
  190.         {
  191.         if (cd->ClassID() == cast)
  192.             return true;
  193.         else
  194.             cd = cd->SuperClass();
  195.         }
  196.     
  197.     return false;
  198.     }
  199.  
  200. //----------------------------------------------------------------------------------------
  201. // TObject::RegisterForDeleteNotification
  202. //----------------------------------------------------------------------------------------
  203. Boolean    TObject::RegisterForDeleteNotification(TCanReceiveDeleteNotification*, Boolean /*shouldRegister*/)
  204.     {
  205.     return false;
  206.     }
  207.  
  208. //----------------------------------------------------------------------------------------
  209. // TObject::FirstBehaviorOfClass: 
  210. //----------------------------------------------------------------------------------------
  211. TBehavior* TObject::FirstBehaviorOfClass(SInt32 classID) const
  212.     {
  213.     TBehavior* behavior = this->FirstBehavior();
  214.     
  215.     if((behavior != nil) && (behavior->DerivedFrom(classID) == false))
  216.         behavior = behavior->NextBehaviorOfClass(classID);
  217.     
  218.     return behavior;
  219.     }
  220.  
  221. //----------------------------------------------------------------------------------------
  222. // TObject::AddBehavior: 
  223. //----------------------------------------------------------------------------------------
  224. void TObject::AddBehavior(TBehavior* behavior)
  225.     {    
  226.     //
  227.     // make sure we don't already have the behavior so we don't get into circular references
  228.     //
  229.     Boolean inList = false;
  230.     
  231.     TBehavior* nextBehavior = this->FirstBehavior();
  232.     while (nextBehavior != nil && inList == false)
  233.         {
  234.         if (behavior == nextBehavior)
  235.             inList = true;
  236.         else
  237.             nextBehavior = nextBehavior->NextBehavior();
  238.         }
  239.     if (!inList)
  240.         {
  241.         behavior->SetNextBehavior(this->FirstBehavior(), this);
  242.         this->SetFirstBehavior(behavior);
  243.         }
  244.     } // TObject::AddBehavior 
  245.  
  246. //----------------------------------------------------------------------------------------
  247. // TObject::RemoveBehavior: 
  248. //----------------------------------------------------------------------------------------
  249. void TObject::RemoveBehavior(TBehavior* behaviorToRemove)
  250.     {    
  251.     // jtr 8/23/94 test the first behavior separately from the rest of the list
  252.     // because changing the first behavior changes the value of fBehavior
  253.     TBehavior* firstBehavior = this->FirstBehavior();
  254.     if(firstBehavior != nil)
  255.         {
  256.         if (firstBehavior == behaviorToRemove)
  257.             {
  258.             this->SetFirstBehavior(firstBehavior->NextBehavior());
  259.             behaviorToRemove->SetNextBehavior(nil, nil);
  260.             }
  261.         else
  262.             {
  263.             Boolean removed = false;
  264.     
  265.             TBehavior* previousBehavior = firstBehavior;                                    // the first behavior
  266.             TBehavior* currentBehavior = firstBehavior->NextBehavior();        // the second behavior
  267.             while (currentBehavior != nil && !removed)
  268.                 {
  269.                 if (currentBehavior == behaviorToRemove)
  270.                     {
  271.                     previousBehavior->SetNextBehavior(currentBehavior->NextBehavior(), this);
  272.                     behaviorToRemove->SetNextBehavior(nil, nil);
  273.                     removed = true;
  274.                     }
  275.                 else
  276.                     currentBehavior = currentBehavior->NextBehavior();
  277.                 }
  278.             }
  279.         }
  280.     } // TObject::RemoveBehavior 
  281.  
  282. //----------------------------------------------------------------------------------------
  283. // TObject::FirstBehavior
  284. //----------------------------------------------------------------------------------------
  285. TBehavior* TObject::FirstBehavior() const
  286.     {
  287.     return nil;
  288.     }
  289.     
  290. //----------------------------------------------------------------------------------------
  291. // TObject::SetFirstBehavior
  292. //----------------------------------------------------------------------------------------
  293. void TObject::SetFirstBehavior(TBehavior* /*firstBehavior*/)
  294.     {
  295.     FailErr(eUnimplemented);
  296.     }
  297.  
  298.  
  299.